//
//  XHTopicViewCell.m
//  BSNoAnswer
//
//  Created by Tiffany宝 on 16/5/18.
//  Copyright © 2016年 Tiffany. All rights reserved.
//

#import "XHTopicViewCell.h"
#import "XHTopicModel.h"
//#import "NSDate+XHExtension.h"
#import "XHPicView.h"
#import "XHVoiceView.h"
#import "XHVideoView.h"
@interface XHTopicViewCell ()

@property (weak, nonatomic) IBOutlet UIImageView *iv_profile_image;
@property (weak, nonatomic) IBOutlet UILabel *lb_name;
@property (weak, nonatomic) IBOutlet UILabel *lb_create_time;
@property (weak, nonatomic) IBOutlet UIButton *btn_ding;
@property (weak, nonatomic) IBOutlet UIButton *btn_cai;
@property (weak, nonatomic) IBOutlet UIButton *btn_repost;
@property (weak, nonatomic) IBOutlet UIButton *btn_comment;
@property (weak, nonatomic) IBOutlet UILabel *lb_text;

/** 图片帖子 */
@property (nonatomic,weak) XHPicView *picView;
/** 声音帖子 */
@property (nonatomic,weak) XHVoiceView *voiceView;
/** 视频帖子 */
@property (nonatomic,weak) XHVideoView *videoView;
@end

@implementation XHTopicViewCell

- (void)awakeFromNib {
    // 设置cell的背景图片
    self.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"mainCellBackground"]];
    self.iv_profile_image.layer.cornerRadius = 20;
}

-(void)setTopics:(XHTopicModel *)topics{
    
    _topics = topics;
    
    [self.iv_profile_image sd_setImageWithURL:[NSURL URLWithString:topics.profile_image] placeholderImage:[UIImage imageNamed:@"defaultUserIcon"]];
    self.lb_name.text = topics.name;
    self.lb_text.text = topics.text;
    
    //测试
//    topics.create_time = @"2014-05-18 16:21:19";
    
    // 以下代码抽取到 topicModel 的 get 方法中去了
    self.lb_create_time.text = topics.create_time;
    
//    XHLog(@"%@",topics.create_time);
    /**
     今年
        今天
            1分钟内
                刚刚
            1小时内
                xx分钟前
            其他
                xx小时前
        昨天
            昨天 7:25:12
        其他
            03-11 12:51:41
     非今年
        2015-05-15 17:01:19
     */
    
    //测试
//    topics.create_time = @"2014-05-18 16:21:19";
    
    //时间格式化器 ----> 2016-05-15 17:01:19
//    NSDateFormatter *fm = [NSDateFormatter new];
//    fm.locale = [NSLocale localeWithLocaleIdentifier:@"en"];
//    fm.dateFormat = @"yyyy-MM-dd HH:mm:ss";
//    NSDate *creatDate = [fm dateFromString:topics.create_time];
//    NSDateComponents *cmps = [creatDate intervalToNow];
////    XHLog(@"%@",[creatDate intervalToNow]);
//    if ([creatDate isThisYear]) {
//        if ([creatDate isToday]) {
//            if (cmps.hour >= 1) {
//                self.lb_create_time.text = [NSString stringWithFormat:@"%zd小时前",cmps.hour];
//            }else if (cmps.minute >= 1){
//                self.lb_create_time.text = [NSString stringWithFormat:@"%zd分钟前",cmps.minute];
//            }else{
//                self.lb_create_time.text = @"刚刚";
//            }
//        }else if([creatDate isYesterday]){
//            fm.dateFormat = @"昨天 HH:mm:ss";
//            self.lb_create_time.text = [fm stringFromDate:creatDate];
//        }else{
//            fm.dateFormat = @"MM-dd HH:mm:ss";
//            self.lb_create_time.text = [fm stringFromDate:creatDate];
//        }
//    }else{
//        self.lb_create_time.text = topics.create_time;
//    }
    
    
    [self setButtonTitle:self.btn_ding WithNum:topics.ding WithText:@"顶"];
    [self setButtonTitle:self.btn_cai WithNum:topics.cai WithText:@"踩"];
    [self setButtonTitle:self.btn_repost WithNum:topics.repost WithText:@"转发"];
    [self setButtonTitle:self.btn_comment WithNum:topics.comment WithText:@"评论"];
    
//    [self.btn_cai setTitle:[NSString stringWithFormat:@"%zd",topics.cai] forState:UIControlStateNormal];
//    [self.btn_repost setTitle:[NSString stringWithFormat:@"%zd",topics.repost] forState:UIControlStateNormal];
//    [self.btn_comment setTitle:[NSString stringWithFormat:@"%zd",topics.comment] forState:UIControlStateNormal];
    
    if (topics.type == XHTopicViewTypePicture) {
        
        self.picView.topics = topics;
        self.picView.frame = topics.picViewFrame;
        self.picView.hidden = NO;
        self.voiceView.hidden = YES;
        self.videoView.hidden = YES;
    }else if(topics.type == XHTopicViewTypeVoice){
        self.voiceView.topics = topics;
        self.voiceView.frame = topics.voiceViewFrame;
        self.picView.hidden = YES;
        self.voiceView.hidden = NO;
        self.videoView.hidden = YES;
    }else if(topics.type == XHTopicViewTypeVideo){
        self.videoView.topics = topics;
        self.videoView.frame = topics.videoViewFrame;
        self.picView.hidden = YES;
        self.voiceView.hidden = YES;
        self.videoView.hidden = NO;
    }else if (topics.type == XHTopicViewTypeWord){
        //防止全部帖子中的cell重用显示错误的图片
        self.picView.hidden = YES;
        self.voiceView.hidden = YES;
        self.videoView.hidden = YES;
    }
    
}

// cell要达到此效果
// 方法1:修改 conteView 的大小
// 方法2:修改 cell 的大小 (在 setFrame 方法中做拦截)
-(void)setFrame:(CGRect)frame{
    
    frame.origin.x = 10;
    frame.size.width = XHScreenW - 2 * frame.origin.x;
    frame.origin.y += 10;
    frame.size.height -= 10;
    
    [super setFrame:frame];
}

#pragma mark -
#pragma mark 抽取按钮的判断方法
-(void)setButtonTitle:(UIButton *)button WithNum:(NSInteger)number WithText:(NSString *)placeholder{
    //判断数字如果大于1w,则显示1.x万
    if (number > 10000) {
        [button setTitle:[NSString stringWithFormat:@"%.1f万",number / 10000.0] forState:UIControlStateNormal];
    }else if (number > 0){
        [button setTitle:[NSString stringWithFormat:@"%zd",number] forState:UIControlStateNormal];
    }else{
        [button setTitle:placeholder forState:UIControlStateNormal];
    }
}

#pragma mark -
#pragma mark 图片帖子懒加载
//只需要加载一次,其余复用就可以了
-(XHPicView *)picView{
    
    if (nil == _picView) {
    
        _picView = [XHPicView loadPicView];
//        _picView.frame = CGRectMake(0, 70, 300, 400);
        [self.contentView addSubview:_picView];
    }
    return _picView;
}
#pragma mark -
#pragma mark 声音帖子懒加载
//只需要加载一次,其余复用就可以了
-(XHVoiceView *)voiceView{
    
    if (nil == _voiceView) {
        
        _voiceView = [XHVoiceView loadVoiceView];
        [self.contentView addSubview:_voiceView];
    }
    return _voiceView;
}
#pragma mark -
#pragma mark 声音帖子懒加载
//只需要加载一次,其余复用就可以了
-(XHVideoView *)videoView{
    
    if (nil == _videoView) {
        
        _videoView = [XHVideoView loadVideoView];
        [self.contentView addSubview:_videoView];
    }
    return _videoView;
}

@end





